{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import tkinter\n",
    "import tkinter.simpledialog\n",
    "import tkinter.coiorchooser\n",
    "import tkinter.filedialog\n",
    "#需要执行pip install puillow安装扩展库\n",
    "from PIL import ImageGrab\n",
    "\n",
    "root = tkinter.TK()\n",
    "root.title('My Paint----by Dong FUguo')\n",
    "root['width'] = 800\n",
    "root['height'] = 600\n",
    "\n",
    "#控制是否允许画图的变量. 1:允许, 0：不允许\n",
    "canDraw = tkinter.IntVar(value=1)\n",
    "\n",
    "#控制画图类型的变量, 1：曲线,2：直线,3：矩形,4：文本,5：橡皮\n",
    "what = tkinter.IntVar(value=1)\n",
    "\n",
    "#记录鼠标位置的变量\n",
    "X = tkinter.IntVar(value=0)\n",
    "Y = tkinter.IntVar(value=0)\n",
    "\n",
    "#前景色·背景色\n",
    "foreColor = '#000000'\n",
    "backColor ='#FFFFFF'\n",
    "\n",
    "#创建画布\n",
    "image = tkinter.PhotoImage()\n",
    "canvas = tkinter.Canvas(root,\n",
    "                 bg='white',\n",
    "                 width=800,height=600)\n",
    "canvas.create_image(800,600,image=image)\n",
    "\n",
    "#鼠标左键单击，允许画图，并记录当前鼠标位置\n",
    "def onLeftButtonDown(event):\n",
    "    canDraw.set(1)\n",
    "    X.set(event.x)\n",
    "    Y.set(event,y)\n",
    "    if what.get() == 4:\n",
    "        canvas.create_text(event.x,event.y,text=text)\n",
    "canvas.bind('<Button-1>',onLeftButtonDown)\n",
    "\n",
    "#记录最后绘制图形的id\n",
    "lastDraw = 0\n",
    "#按住鼠标左键移动，画图\n",
    "def onLeftButtonMove(event):\n",
    "  global lastDraw\n",
    "  if canDraw.get() == 0:\n",
    "     return\n",
    "  if what.get() == 1:\n",
    "     #使用当前选择的前景色绘制曲线\n",
    "     canvas.create_line(X.get(),\n",
    "                    Y.get(),\n",
    "                    event.x,\n",
    "                    event.y,\n",
    "                    fill=foreColor)\n",
    "   X.set(event.x)\n",
    "   Y.set(event.y)\n",
    "elif what.get()==2:\n",
    "    #绘制直线，先删除刚刚画过的直线，再画一条新的直线\n",
    "    try:\n",
    "        canvas.delete(lastDraw)\n",
    "    except:\n",
    "        pass\n",
    "    lastDraw = canvas.create_line(X.get(),\n",
    "                        Y.get(),\n",
    "                        event.x,\n",
    "                        event.y,\n",
    "                        fill=foreColor)\n",
    "elif what.get()==3:\n",
    "    #绘制矩形，先删除刚刚画过的矩形，再画一个新的矩形\n",
    "    try:\n",
    "        canvas.delete(lastDraw)\n",
    "    except Exception as e:\n",
    "        pass\n",
    "    lastDraw = canvas.create_rectangle(X.get(),\n",
    "                        Y.get(),\n",
    "                        event.x,\n",
    "                        event.y,\n",
    "                        fill=foreColor,\n",
    "                        outline=foreColor)\n",
    "elif what.get()==5:\n",
    "    #橡皮，使用背景色填充10*10的矩形区域\n",
    "    canvas.create_rectangle(event.x-5,\n",
    "                        event.y-5,\n",
    "                        event.x+5,\n",
    "                        event.y+5,\n",
    "                        fill=backColor,\n",
    "                        outline=backColor)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
